package com.gf.framework.web.config;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ConverterKeyBuild;
import com.alibaba.excel.converters.DefaultConverterLoader;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.gf.framework.common.utils.DateUtil;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @author gaofei
 * @date 2022/6/8 21:04
 */
@Configuration
@Component
public class ExcelConfig {
    /**
     * excel导出增强，对LocalDate  LocalDateTime LocalTime 类型，做增强处理
     */
    static {
        Converter<LocalDate> localDateExcelConverter = new Converter<LocalDate>() {
            @Override
            public Class<LocalDate> supportJavaTypeKey() {
                return LocalDate.class;
            }

            @Override
            public CellDataTypeEnum supportExcelTypeKey() {
                return CellDataTypeEnum.STRING;
            }

            @Override
            public LocalDate convertToJavaData(
                    CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return LocalDate.parse(cellData.getStringValue(),
                        DateTimeFormatter.ofPattern(DateUtil.DATE_PATTERN));
            }

            @Override
            public CellData<String> convertToExcelData(
                    LocalDate localDate, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return new CellData<>(localDate.format(DateTimeFormatter.ofPattern(DateUtil.DATE_PATTERN)));
            }
        };
        DefaultConverterLoader.loadDefaultWriteConverter().put(ConverterKeyBuild.buildKey(LocalDate.class), localDateExcelConverter);

        Converter<LocalDateTime> localDateTimeExcelConverter = new Converter<LocalDateTime>() {
            @Override
            public Class<LocalDateTime> supportJavaTypeKey() {
                return LocalDateTime.class;
            }

            @Override
            public CellDataTypeEnum supportExcelTypeKey() {
                return CellDataTypeEnum.STRING;
            }

            @Override
            public LocalDateTime convertToJavaData(
                    CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return LocalDateTime.parse(cellData.getStringValue(),
                        DateTimeFormatter.ofPattern(DateUtil.DATE_TIME_PATTERN));
            }

            @Override
            public CellData<String> convertToExcelData(
                    LocalDateTime localDateTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return new CellData<>(localDateTime.format(
                        DateTimeFormatter.ofPattern(DateUtil.DATE_TIME_PATTERN)));
            }
        };

        DefaultConverterLoader.loadDefaultWriteConverter().put(ConverterKeyBuild.buildKey(LocalDateTime.class), localDateTimeExcelConverter);

        Converter<LocalTime> localTimeExcelConverter = new Converter<LocalTime>() {
            @Override
            public Class<LocalTime> supportJavaTypeKey() {
                return LocalTime.class;
            }

            @Override
            public CellDataTypeEnum supportExcelTypeKey() {
                return CellDataTypeEnum.STRING;
            }

            @Override
            public LocalTime convertToJavaData(
                    CellData cellData, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return LocalTime.parse(cellData.getStringValue(),
                        DateTimeFormatter.ofPattern(DateUtil.TIME_PATTERN));
            }

            @Override
            public CellData<String> convertToExcelData(
                    LocalTime localTime, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) {
                return new CellData<>(localTime.format(
                        DateTimeFormatter.ofPattern(DateUtil.TIME_PATTERN)));
            }
        };

        DefaultConverterLoader.loadDefaultWriteConverter().put(ConverterKeyBuild.buildKey(LocalTime.class), localTimeExcelConverter);
    }
}
